package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mifengkong.frtools.inputmethod.FRInputMethodManager;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CashByCreditCardBean;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;


/**
 * Created by jack on 2017/6/2
 */

public class CashMoneyUIComponent extends LinearLayout implements View.OnClickListener {

    public CashMoneyUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private EditText etMoney;
    private ImageView ivClear;

    private double totalLeave;
    private double mAmount;
    private int perCardLimit;
    private int dayCardLimit;
    private String fee;
    private String perFee;
    private OnMoneyInputListener onMoneyInputListener;

    private SelectFeeUIComponent selectFeeUIComponent;

    private boolean isAllowClick;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        etMoney = findViewById(R.id.lca_et_money);
        ivClear = findViewById(R.id.lca_iv_clear);
        selectFeeUIComponent = findViewById(R.id.lca_select_fee);

        initListener();
    }

    private void initListener() {
        selectFeeUIComponent.setViewLineVisible(false);
        etMoney.setFocusable(false);
        etMoney.setFocusableInTouchMode(false);
        etMoney.setOnClickListener(this);
        ivClear.setOnClickListener(this);

        etMoney.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        etMoney.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});

        etMoney.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                String mInputAmount = s.toString();
                selectFeeUIComponent.setDescContent(fee, selectFeeUIComponent.getServiceChange(fee, getMoney()), perFee, isAllowClick);
                if (null != onMoneyInputListener) {
                    onMoneyInputListener.getMoney();
                }
                if (FRString.isEmpty(mInputAmount)) {
                    ivClear.setVisibility(View.GONE);
                    return;
                }
                ivClear.setVisibility(View.VISIBLE);
                mAmount = Double.valueOf(mInputAmount);
                if (totalLeave <= 0) {   //今日剩余额度为0
                    FRToast.showToastSafe((String) TextUtils.concat("单卡单日限额不能超过", String.valueOf(dayCardLimit * 1.0 / 10000), "万，建议您换张卡试试。"));
                    setMoneyContent("");
                } else {
                    if (totalLeave > perCardLimit) {     //剩余额度大于单笔消费额度
                        if (mAmount > perCardLimit) {
                            setPerCardLimit();
                        }
                    } else {    //单笔消费额度大于剩余额度
                        if (mAmount > totalLeave) {
                            FRToast.showToastSafe((String) TextUtils.concat("单卡单日限额不能超过", String.valueOf(dayCardLimit * 1.0 / 10000), "万"));
                            setMoneyContent(FRString.valueOf(totalLeave));
                        }
                    }
                }
            }
        });
    }

    public void setFocusa() {
        etMoney.setFocusable(true);
        etMoney.setFocusableInTouchMode(true);
        etMoney.requestFocus();
    }

    public void setFeeContent(CashByCreditCardBean.PassWay passWay) {
        this.fee = passWay.getFee();
        this.perFee = passWay.getPer_fee();
        this.isAllowClick = CommonUtil.isStatusSuccess(passWay.getAllow_click());
        passWay.setMoney(getMoney());
        selectFeeUIComponent.setFeeContent(passWay);
    }

    private void setPerCardLimit() {
        FRToast.showToastSafe(FRCollection.appendAsString("单笔消费不能超过", FRString.valueOf(perCardLimit * 1.0 / 10000), "万", ""));
        setMoneyContent(FRString.valueOf(perCardLimit));
    }

    public void setTotalLeave(double totalLeave) {
        this.totalLeave = totalLeave;
        if (!FRString.isEmpty(getMoney()) && Double.valueOf(getMoney()) > totalLeave) {
            etMoney.setText(FRString.valueOf(totalLeave));
            FRToast.showToastSafe((String) TextUtils.concat("单卡单日限额不能超过", String.valueOf(dayCardLimit * 1.0 / 10000), "万，建议您换张卡试试。"));
        }
    }

    public void setPerCardLimit(int perCardLimit) {
        this.perCardLimit = perCardLimit;
    }

    public void setDayCardLimit(int dayCardLimit) {
        this.dayCardLimit = dayCardLimit;
    }

    public void setChangeClickListener(OnClickListener onClickListener) {
        selectFeeUIComponent.setChangeContent(onClickListener);
    }

    public String getMoney() {
        return etMoney.getText().toString().trim();
    }

    public void setMoney(String money) {
        etMoney.setText(FRString.valueOf(money));
    }

    public boolean isMoneyCorrect() {
        if (FRString.isEmpty(getMoney()) || Double.valueOf(getMoney()) == 0) {
            FRToast.showToastSafe("请输入金额");
            return false;
        }
        if (Double.valueOf(getMoney()) > totalLeave) {
            FRToast.showToastSafe("当前取现金额大于你的最大额度，请重新输入");
            return false;
        }
        return true;
    }

    private void setMoneyContent(String money) {
        etMoney.setText(FRString.valueOf(money));
        if (money.length() >= 0) {
            etMoney.setSelection(money.length());
        }
    }

    public void setOnMoneyInputListener(OnMoneyInputListener onMoneyInputListener) {
        this.onMoneyInputListener = onMoneyInputListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lca_et_money:
                if (!etMoney.isFocusable()) {
                    etMoney.setFocusable(true);
                    etMoney.setFocusableInTouchMode(true);
                    etMoney.requestFocus();
                    FRInputMethodManager.showInputMethod(getContext(), etMoney);
                }
                break;
            case R.id.lca_iv_clear:
                etMoney.setText("");
                ivClear.setVisibility(View.GONE);
                break;
        }
    }

    public interface OnMoneyInputListener {
        void getMoney();
    }
}